//TeacherServiceImpl.java
package org.example.demo3.service.impl;

import org.example.demo3.model.Teacher;
import org.example.demo3.mapper.TeacherMapper;
import org.example.demo3.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherMapper teacherMapper;

    //返回教师列表
    @Override
    public List<Teacher> findAll() {
        return teacherMapper.selectAll();
    }

    //删除教师
    @Override
    public int remove(int id) {
        return teacherMapper.deleteById(id);
    }

    //添加教师
    @Override
    public int add(Teacher teacher) {
        return teacherMapper.insert(teacher);
    }

    //查询对应id的教师
    @Override
    public Teacher findOneById(int id){
        return teacherMapper.selectById(id);
    }

    //修改教师
    @Override
    public int edit(Teacher teacher) {
        return teacherMapper.update(teacher);
    }

    //分页查询
    @Override
    public List<Teacher> findByPage(int currentPage, int pageSize){
        //如果页号<1 , 则从第一条记录开始，以防异常
        int formIndex = (currentPage < 1) ? 0 : (currentPage - 1) * pageSize;
        return teacherMapper.selectByPage(formIndex,pageSize);
    }

    //登录验证
    @Override
    public boolean login(String username, String password) {
        //如果数据库返回不为空，则为true，否则为false
        return teacherMapper.selectByNameAndPwd(username,password) != null;
    }
}
